package db

import (
	"d-serv-go/server/core/bean"
	"d-serv-go/server/core/cache"
	"d-serv-go/server/core/consts"
	"d-serv-go/server/core/util"
	"reflect"
	"strings"
)

type UserBean struct {
	bean.Bean
	cmpyBean  *bean.CmpyBean
	oDeptBean *bean.DeptBean
}

func NewUserBean() *UserBean {
	return &UserBean{Bean: *bean.NewBean("")}
}

func NewUserBean2(bean *bean.Bean) *UserBean {
	u := &UserBean{Bean: *bean}
	u.SetId(bean.GetStr("USER_CODE"))
	return u
}

func (u UserBean) IsStructEmpty() bool {
	return reflect.DeepEqual(u, UserBean{})
}

func (u UserBean) GetPassword() string {
	return u.GetStr("USER_PASSWORD")
}

func (u UserBean) GetCmpyCode() string {
	return u.GetStr("CMPY_CODE")
}

func (u UserBean) GetCode() string {
	return u.GetStr("USER_CODE")
}

func (u UserBean) GetName() string {
	return u.GetStr("USER_NAME")
}

func (u UserBean) GetLoginName() string {
	return u.GetStr("USER_LOGIN_NAME")
}

func (u UserBean) GetPost() string {
	return u.GetStr("USER_POST")
}

func (u UserBean) GetIdcard() string {
	return u.GetStr("USER_IDCARD")
}

func (u UserBean) GetSex() int {
	return u.GetInt("USER_SEX", 0)
}

func (u UserBean) GetBirthday() string {
	return u.GetStr("USER_BIRTHDAY")
}

func (u UserBean) GetMobile() string {
	return u.GetStr("USER_MOBILE")
}

func (u UserBean) GetImg() string {
	return u.GetStr("USER_IMG")
}

func (u UserBean) GetImgSrc() string {
	return u.GetStr("USER_IMG_SRC")
}

func (u UserBean) GetDeptCode() string {
	return u.GetStr("DEPT_CODE")
}

func (u UserBean) GetDeptName() string {
	return u.GetStr("DEPT_NAME")
}

func (u UserBean) GetDeptSrcType1() string {
	return u.GetStr("USER_IMG")
}

func (u UserBean) GetDeptSrcType2() string {
	return u.GetStr("USER_IMG")
}

func (u UserBean) GetDeptLine() string {
	return u.GetStr("USER_IMG")
}

func (u UserBean) GetTDeptCode() string {
	return u.GetStr("USER_IMG")
}

func (u UserBean) GetTDeptName() string {
	return u.GetStr("USER_IMG")
}

func (u UserBean) GetEmail() string {
	return u.GetStr("USER_EMAIL")
}

func (u UserBean) GetODeptCode() string {
	return u.GetStr("ODEPT_CODE")
}

func (u UserBean) GetODeptName() string {
	return u.GetODeptBean().GetName()
}

func (u UserBean) GetCodePath() string {
	return u.GetStr("CODE_PATH")
}

func (u UserBean) GetRoleCodes() []string {
	userExt := u.GetUserExt()
	if s, ok := userExt.Get(consts.ROLE_CODES).([]string); ok {
		return s
	}
	return make([]string, 0)
}

func (u UserBean) GetRoleCodeQuotaStr() string {
	return "'" + strings.Join(u.GetRoleCodes(), "','") + "'"
}

func (u UserBean) GetRoleCodeQuotaStr0() string {
	return strings.Join(u.GetRoleCodes(), ",")
}

func (u UserBean) GetDeptCodeQuotaStr() string {
	oDeptCode := u.GetODeptCode()
	codePath := u.GetCodePath()
	pos := strings.Index(codePath, oDeptCode)
	if pos > 0 {
		codePath = codePath[pos:]
	}
	return "'" + strings.ReplaceAll(codePath, "\\"+consts.CODE_PATH_SEPERATOR, "','") + "'"
}

func (u UserBean) GetGroupCodeQuotaStr() string {
	return "'" + strings.Join(u.GetGroupCodes(), "','") + "'"
}

func (u UserBean) GetGroupCodes() []string {
	userExt := u.GetUserExt()
	if s, ok := userExt.Get(consts.GROUP_CODES).([]string); ok {
		return s
	}
	return make([]string, 0)
}

func (u UserBean) GetAdminGroupCodeQuotaStr() string {
	return "'" + strings.Join(u.GetAdminGroupCodes(), "','") + "'"
}

func (u UserBean) GetAdminGroupCodes() []string {
	userExt := u.GetUserExt()
	if s, ok := userExt.Get(consts.ADMIN_GROUP_CODES).([]string); ok {
		return s
	}
	return make([]string, 0)
}

func (u UserBean) GetUserExt() *bean.Bean {
	if value, ok := cache.CacheMgr.Get(u.GetCode(), consts.CACHE_USER_EXT).(*bean.Bean); ok {
		return value
	}
	userExt := bean.NewBean("")
	// 设置角色信息
	userExt.Set(consts.ROLE_CODES, RoleMgr.GetRoleCodes(u.GetCode(), u.GetCmpyCode(), u.GetODeptLevel()))
	// 设置群组信息
	param := NewSqlBean()
	param.Selects("distinct GROUP_CODE, GU_ADMIN")
	param.And("USER_CODE", u.GetCode())
	param.And("S_FLAG", consts.YES_INT)
	param.And("GROUP_FLAG", consts.YES_INT)
	param.And("USER_FLAG", consts.YES_INT)
	groups := make([]string, 0)
	adminGroups := make([]string, 0)
	ServDao.FindsCall2(consts.SY_ORG_GROUP_USER, NewParamBean(param.Bean), func(columns []*bean.Bean, data *bean.Bean) int {
		groups = append(groups, data.GetStr("GROUP_CODE"))
		if data.GetInt("GU_ADMIN") == consts.YES_INT { // 当前人负责管理的
			adminGroups = append(adminGroups, data.GetStr("GROUP_CODE"))
		}
		return 1
	},
	)
	userExt.Set(consts.GROUP_CODES, groups)
	userExt.Set(consts.ADMIN_GROUP_CODES, adminGroups)
	cache.CacheMgr.Set(u.GetCode(), userExt, consts.CACHE_USER_EXT) // 放置到缓存中
	return userExt
}

func (u UserBean) ClearUserExt() {
	cache.CacheMgr.Remove(u.GetCode(), consts.CACHE_USER_EXT)
}

func (u UserBean) GetODeptBean() *bean.DeptBean {
	if u.oDeptBean == nil {
		oDeptCode := u.GetODeptCode()
		if len(oDeptCode) == 0 {
			oDeptCode = u.GetDeptCode()
		}
		u.oDeptBean = OrgMgr.GetDept(oDeptCode)
		if u.oDeptBean == nil {
			u.oDeptBean = bean.NewDeptBean()
		}
	}
	return u.oDeptBean
}

func (u UserBean) GetODeptLevel() int {
	return u.GetODeptBean().GetLevel()
}

func (u UserBean) GetODeptFullName() string {
	return u.GetODeptBean().GetFullName()
}

func (u UserBean) GetODeptCodePath() string {
	return u.GetODeptBean().GetCodePath()
}

func (u UserBean) GetParentODeptCode() string {
	pCode := u.GetODeptBean().GetPcode()
	if len(pCode) > 0 {
		return OrgMgr.GetDept(pCode).GetODeptCode()
	} else {
		return ""
	}
}

func (u UserBean) IsActivity() bool {
	if u.GetInt("S_FLAG") != consts.YES_INT { // 用户未起用
		return false
	}
	if u.GetInt("USER_STATE") != consts.YES_INT { // 用户不在职
		return false
	}
	exp := u.GetStr("USER_EXPIRE_DATE")
	if u.IsNotEmpty("USER_EXPIRE_DATE") && util.DateUtil.Before(exp, util.DateUtil.GetDateTime()) {
		return false // 有效期过期
	}
	return true
}
